<!--
 * @Descripttion: (/cks_barcode_rel_rec)
 * @Author: (admin)
 * @Date: (2025-08-27)
-->
<template>
  <div>
    <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>

      <el-form-item :label="$t('review.cksbarcoderelrecPage.page.primaryCode')" prop="primaryCode">
        <el-input v-model="queryParams.primaryCode" @keyup.enter="handleQuery"
          :placeholder="$t('review.cksbarcoderelrecPage.page.mprimaryCodeInput')" clearable style="width: 400px" />
      </el-form-item>

      <el-form-item :label="$t('review.cksbarcoderelrecPage.page.subcodeCode')" prop="subcodeCode">
        <el-input v-model="queryParams.subcodeCode" @keyup.enter="handleQuery"
          :placeholder="$t('review.cksbarcoderelrecPage.page.subcodeCodeInput')" clearable style="width: 300px" />
      </el-form-item>

      <el-form-item>
        <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
        <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
      </el-form-item>
    </el-form>
    <!-- 工具区域 -->
    <el-row :gutter="15" class="mb10">
      <el-col :span="1.5">
        <el-button type="primary" v-hasPermi="['cksbarcoderelrec:add']" plain icon="plus" @click="handleAdd">
          {{ $t('btn.add') }}
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button type="danger" :disabled="multiple" v-hasPermi="['cksbarcoderelrec:delete']" plain icon="delete"
          @click="handleDelete">
          {{ $t('btn.delete') }}
        </el-button>
      </el-col>
      <!-- <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar> -->
    </el-row>

    <el-table :data="dataList" v-loading="loading" ref="table" border header-cell-class-name="el-table-header-cell"
      highlight-current-row @sort-change="sortChange" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />

      <el-table-column prop="id" label="ID" align="center" width="80" v-if="columns.showColumn('id')" />

      <el-table-column prop="lineCode" :label="$t('review.cksbarcoderelrecPage.tableColumns.lineCode')" align="center"
        width="180" v-if="columns.showColumn('lineCode')" />

      <el-table-column prop="stationCode" :label="$t('review.cksbarcoderelrecPage.tableColumns.stationCode')"
        align="center" width="180" v-if="columns.showColumn('stationCode')" />

      <el-table-column prop="readDB" :label="$t('review.cksbarcoderelrecPage.tableColumns.readDB')" align="center"
        width="180" v-if="columns.showColumn('readDB')" />

      <el-table-column prop="primaryCode" :label="$t('review.cksbarcoderelrecPage.tableColumns.primaryCode')"
        align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('primaryCode')" />

      <el-table-column prop="subcodeCode" :label="$t('review.cksbarcoderelrecPage.tableColumns.subcodeCode')"
        align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('subcodeCode')" />

      <el-table-column prop="type" :label="$t('review.cksbarcoderelrecPage.tableColumns.type')" align="center"
        v-if="columns.showColumn('type')">
        <template #default="scope">
          <dict-tag :options="options.typeOptions" :value="scope.row.type" />
        </template>
      </el-table-column>

      <el-table-column prop="isDeleted" :label="$t('review.cksbarcoderelrecPage.tableColumns.isDeleted')" align="center"
        v-if="columns.showColumn('type')">
        <template #default="scope">
          <el-tag type="warning"
            v-if="scope.row.isDeleted">{{$t('review.cksbarcoderelrecPage.formDialog.hasunBinding')}}</el-tag>
          <el-tag type="primary" v-else>{{$t('review.cksbarcoderelrecPage.formDialog.hasBinding')}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column prop="createdTime" :label="$t('review.cksbarcoderelrecPage.tableColumns.createdTime')"
        align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('createdTime')" />

      <el-table-column :label="$t('btn.operate')" width="200" align="center">
        <template #default="scope">
          <el-button type="success" size="small" icon="edit" v-hasPermi="['cksbarcoderelrec:edit']"
            @click="handleUpdate(scope.row)">{{ $t("btn.edit") }}</el-button>
          <el-button type="danger" size="small" icon="delete" v-hasPermi="['cksbarcoderelrec:delete']"
            @click="handleDelete(scope.row)">{{ $t("btn.delete") }}</el-button>
        </template>
      </el-table-column>

    </el-table>
    <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :lock-scroll="false" v-model="open">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="150px">
        <el-row :gutter="20">
          <!-- <el-col :lg="12" v-if="opertype != 1">
            <el-form-item label="ID" prop="id">
              <el-input-number v-model.number="form.id" controls-position="right" placeholder="请输入ID" :disabled="true"/>
            </el-form-item>
          </el-col> -->

          <el-col :lg="12">
            <el-form-item :label="$t('review.cksbarcoderelrecPage.tableColumns.plcId')" prop="plcID">
              <el-input v-model.number="form.plcID" :placeholder="$t('review.cksbarcoderelrecPage.formDialog.plcId')" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item :label="$t('review.cksbarcoderelrecPage.tableColumns.readDB')" prop="readDB">
              <el-input v-model.number="form.readDB"
                :placeholder="$t('review.cksbarcoderelrecPage.formDialog.readDB')" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item :label="$t('review.cksbarcoderelrecPage.tableColumns.primaryCode')" prop="primaryCode">
              <el-input v-model="form.primaryCode"
                :placeholder="$t('review.cksbarcoderelrecPage.formDialog.primaryCode')" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item :label="$t('review.cksbarcoderelrecPage.tableColumns.subcodeCode')" prop="subcodeCode">
              <el-input v-model="form.subcodeCode"
                :placeholder="$t('review.cksbarcoderelrecPage.formDialog.subcodeCode')" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item :label="$t('review.cksbarcoderelrecPage.tableColumns.type')" prop="type">
              <el-select style="width: 450px;" v-model="form.type"
                :placeholder="$t('review.cksbarcoderelrecPage.formDialog.type')">
                <el-option v-for="item in options.typeOptions" :key="item.dictValue" :label="item.dictLabel"
                  :value="item.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>


          <el-col :lg="12">
            <el-form-item :label="$t('review.cksbarcoderelrecPage.tableColumns.isDeleted')" prop="isDeleted">
              <el-radio-group v-model="form.isDeleted">
                <el-radio :value="true">{{$t('review.cksbarcoderelrecPage.formDialog.hasunBinding')}}</el-radio>
                <el-radio :value="false">{{$t('review.cksbarcoderelrecPage.formDialog.hasBinding')}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>
      <template #footer v-if="opertype != 3">
        <el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="submitForm">{{ $t('btn.submit') }}</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="cksbarcoderelrec">
import {
  listCksBarcodeRelRec,
  addCksBarcodeRelRec, delCksBarcodeRelRec,
  updateCksBarcodeRelRec, getCksBarcodeRelRec,
}
  from '@/api/business/cksbarcoderelrec.js'
import { number } from 'echarts'
const { proxy } = getCurrentInstance()
const ids = ref([])
const loading = ref(false)
const showSearch = ref(true)
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  sort: '',
  sortType: 'asc',
  primaryCode: "",
  subcodeCode: "",
  lineCode: "",
  stationCode: "",
})
const columns = ref([
  { visible: true, prop: 'id', label: 'ID' },
  { visible: true, prop: 'lineCode', label: '产线' },
  { visible: true, prop: 'stationCode', label: '工站' },
  { visible: true, prop: 'readDB', label: 'DB块' },
  { visible: true, prop: 'primaryCode', label: '主码' },
  { visible: true, prop: 'subcodeCode', label: '子码' },
  { visible: true, prop: 'type', label: '类型' },
  { visible: true, prop: 'createdTime', label: '创建时间' },
  { visible: false, prop: 'isDeleted', label: '逻辑删除' },
])
const total = ref(0)
const dataList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])


var dictParams = [
]


function getList() {
  loading.value = true
  listCksBarcodeRelRec(queryParams).then(res => {
    const { code, data } = res
    if (code == 200) {
      dataList.value = data.result
      total.value = data.totalNum
      loading.value = false
    }
  })
}

// 查询
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置查询操作
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map((item) => item.id);
    single.value = selection.length != 1
    multiple.value = !selection.length;
}

// 自定义排序
function sortChange(column) {
  var sort = undefined
  var sortType = undefined

  if (column.prop != null && column.order != null) {
    sort = column.prop
    sortType = column.order

  }
  queryParams.sort = sort
  queryParams.sortType = sortType
  handleQuery()
}

/*************** form操作 ***************/
const formRef = ref()
const title = ref('')
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
  single: true,
  multiple: true,
  form: {},
  rules: {
    plcID: [{ required: true, message: proxy.$t('review.cksbarcoderelrecPage.formDialogVerify.plcId'), trigger: "blur" }],
    readDB: [{ required: true, message: proxy.$t('review.cksbarcoderelrecPage.formDialogVerify.readDB'), trigger: "blur" }],
    primaryCode: [{ required: true, message: proxy.$t('review.cksbarcoderelrecPage.formDialogVerify.primaryCode'), trigger: "blur" }],
    subcodeCode: [{ required: true, message: proxy.$t('review.cksbarcoderelrecPage.formDialogVerify.subcodeCode'), trigger: "blur" }],
    type: [{ required: true, message: proxy.$t('review.cksbarcoderelrecPage.formDialogVerify.type'), trigger: "change" }],
  },
  options: {
    // 类型 99:托盘码.... 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
    typeOptions: [
      { dictLabel: proxy.$t('review.cksbarcoderelrecPage.bindType.type1'), dictValue: '1' },
      { dictLabel: proxy.$t('review.cksbarcoderelrecPage.bindType.type2'), dictValue: '2' },
      { dictLabel: proxy.$t('review.cksbarcoderelrecPage.bindType.type101'), dictValue: '101' },
      { dictLabel: proxy.$t('review.cksbarcoderelrecPage.bindType.type102'), dictValue: '102' },
      { dictLabel: proxy.$t('review.cksbarcoderelrecPage.bindType.type103'), dictValue: '103' },
      { dictLabel: proxy.$t('review.cksbarcoderelrecPage.bindType.type104'), dictValue: '104' },
      { dictLabel: proxy.$t('review.cksbarcoderelrecPage.bindType.type105'), dictValue: '105' },
      { dictLabel: proxy.$t('review.cksbarcoderelrecPage.bindType.type106'), dictValue: '106' },
      { dictLabel: proxy.$t('review.cksbarcoderelrecPage.bindType.type99'), dictValue: '99' }
    ],
  }
})

const { form, rules, options, single, multiple } = toRefs(state)

// 关闭dialog
function cancel() {
  open.value = false
  reset()
}

// 重置表单
function reset() {
  form.value = {
    id: null,
    plcID: null,
    readDB: null,
    primaryCode: null,
    subcodeCode: null,
    type: null,
    createdTime: null,
    isDeleted: null,
  };
  proxy.resetForm("formRef")
}


// 添加按钮操作
function handleAdd() {
  reset();
  open.value = true
  title.value = proxy.$t('review.cksbarcoderelrecPage.formDialog.addTitle')
  opertype.value = 1
}
// 修改按钮操作
function handleUpdate(row) {
  reset()
  const id = row.id || ids.value
  getCksBarcodeRelRec(id).then((res) => {
    const { code, data } = res
    if (code == 200) {
      open.value = true
      title.value = proxy.$t('review.cksbarcoderelrecPage.formDialog.editTitle')
      opertype.value = 2
      data.type = data.type+"";
      form.value = {
        ...data,
      }
    }
  })
}

// 添加&修改 表单提交
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {

      if (form.value.id != undefined && opertype.value === 2) {
        updateCksBarcodeRelRec(form.value).then((res) => {
          proxy.$modal.msgSuccess(proxy.$t('common.operateSuccess'))
          open.value = false
          getList()
        })
      } else {
        addCksBarcodeRelRec(form.value).then((res) => {
          proxy.$modal.msgSuccess(proxy.$t('common.operateSuccess'))
          open.value = false
          getList()
        })
      }
    }
  })
}

// 删除按钮操作
function handleDelete(row) {
  const Ids = row.id || ids.value

  proxy
    .$confirm(proxy.$t("common.deleteConfirm",[Ids]), proxy.$t("common.systemTips"), {
      confirmButtonText: proxy.$t('common.ok'),
      cancelButtonText: proxy.$t('common.cancel'),
      type: "warning",
    })
    .then(function () {
      return delCksBarcodeRelRec(Ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess(proxy.$t('common.operateSuccess'))
    })
}


handleQuery()
</script>